import sys
#sys.path.append('C:\Users\Pam\Desktop\Python_Program\PhysContr_1.0-RF-AM\src')
#from pyvisa.visa import *
from ..visa import *
from GPIB_upperclass import GPIB_wrapper

import string

def strParse(str):
	if "e" in str:
		temp=str.split("e")
		return float(temp[0])*10**(int(temp[1]))
	elif "E" in str:
		temp=str.split("E")
		return float(temp[0])*10**(int(temp[1]))
	elif str !='':
		return float(str)
		
class SR830:
	
	def __init__(self,add,test=False):
		"""Constructor"""
		self.isTest=test
		self.address=add
		self.gpib=GPIB_wrapper(add,test)
		self.measure_rates={512:13,256:12,128:11,64:10,32:9,16:8,\
					8:7,4:6,2:5,1:4,0.5:3,0.25:2,0.125:1,0.0625:0}#Hz
		self.measure_sensitivities={1:26,0.5:25,0.2:24,0.1:23,0.05:22,\
					0.02:21,0.01:20,0.005:19,0.002:18,0.001:17,0.0005:16,\
					0.0002:15,0.0001:14,0.00005:13,0.00002:12,0.00001:11,\
					0.000005:10,0.000002:9,0.000001:8,0.0000005:7,\
					0.0000002:6,0.0000001:5,0.00000005:4,0.00000002:3,\
					0.00000001:2,0.000000005:1,0.000000002:0}#V
		self.measure_time_constants={30000:19,10000:18,3000:17,1000:16,\
					300:15,100:14,30:13,10:12,3:11,1:10,0.3:9,0.1:8,\
					0.03:7,0.01:6,0.003:5,0.001:4,0.0003:3,0.0001:2,\
					0.00003:1,0.00001:0}#s
		self.measure_LPF={6:0,12:1,18:2,24:3} #dB/oct
	
	def Start_Measure(self):
		"""docstring for Start_Measure"""
		command="STRT"
		self.gpib.write(command)
	
	def Stop_Measure(self):
		"""docstring for Pause_Measure"""
		command="PAUS"
		self.gpib.write(command)
		
	def Read_XY(self):
		"""docstring for Pause_Measure"""
		command="OUTP ? 1"
		self.gpib.write(command)
		X=self.gpib.read("scientific")
		command="OUTP ? 2"
		self.gpib.write(command)
		Y=self.gpib.read("scientific")
		return X,Y
	
	def Read_Points(self,i):		
		"""
		Parameters:
			- i: channel = 1, 2
		"""
		query=self.Query_N_Points()
		query=int(query)
		command="TRCA ? %d,0,%d" %(i,query)

		self.gpib.write(command)
		return self.gpib.read("list,scientific")
			
	def Query_N_Points(self):
		"""docstring for Reset_Memory"""
		command="SPTS ?"
		self.gpib.write(command)
		return self.gpib.read("int")
			
	def Reset_Memory(self):
		"""docstring for Reset_Memory"""
		command="REST"
		self.gpib.write(command)
	
	
	def Set_LPF(self, slope):
		"""docstring for Set_LPF"""
		command="OFSL %d" %self.measure_LPF[slope]
		self.gpib.write(command)
	
	def Set_Sensitivity(self,sensitivity):
		"""docstring for Set_Sensitivity"""
		command="SENS %d" %self.measure_sensitivities[sensitivity]
		self.gpib.write(command)
	
	def Set_Time_Constant(self,time_constant):
		"""docstring for Set_Time_Constant"""
		command="OFLT %d" %self.measure_time_constants[time_constant]
		self.gpib.write(command)
	
	def Set_InterExter_Mode(self,num):#num=0->External modulation
		"""docstring for InterExter_Mode"""
		command="FMOD %d" %num
		self.gpib.write(command)
	
	def Set_Harm(self,harmo):
		"""docstring for Harm"""
		command="HARM %d" %harmo
		self.gpib.write(command)
	
	def Set_Measure_Rate(self,rate):
		"""docstring for Set_Measure_Rate"""
		command1="SEND 0"
		command2="SRAT %d" % self.measure_rates[rate]
		self.gpib.write(command1)
		self.gpib.write(command2)
		
	def Set_Freq(self,freq):
		"""docstring for Set_freq"""
		command = "FREQ %d" % freq
		self.gpib.write(command)
